<!-- 订单列表 -->
<template>
  <div class="order-list">
    <!-- 订单列表的标题 -->
    <div class="order-list__title">
      <h2>订单列表</h2>
    </div>
    <!-- 订单列表的内容 -->
    <div class="order-list__content">
      <el-table :data="orderList" style="width: 100%" :default-sort="{prop: 'createTime', order: 'ascending'}">
        <el-table-column prop="orderNumber" label="订单编号" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="name" label="项目名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="masterName" label="工作人员" width="80"></el-table-column>
        <el-table-column prop="date" label="日期" :show-overflow-tooltip="true" sortable></el-table-column>
        <el-table-column prop="time" label="时间" :show-overflow-tooltip="true">
          <template slot-scope="{row}">
            <span type="primary" v-if="row.time == 'afternoon'">下午（13:30-15:00）</span>
            <span type="primary" v-else-if="row.time == 'morning'">早上（9:00-12:00）</span>
            <span type="primary" v-else>晚上（19:00-21:00）</span>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="具体地址" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="phone" label="联系方式" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="createTime" label="订单创建时间" width="220" sortable></el-table-column>
        <el-table-column prop="status" label="订单状态" width="100" sortable>
          <template slot-scope="{row}">
            <el-tag type="primary" v-if="row.status == 0">进行中</el-tag>
            <el-tag type="success" v-else-if="row.status == 1">已结束</el-tag>
            <el-tag type="info" v-else-if="row.status == 2">已取消</el-tag>
            <el-tag type="danger" v-else>申诉中</el-tag>
          </template>
        </el-table-column>
        <el-table-column>
          <template slot="header" slot-scope="scope">
            <el-button type="primary" size="small" @click="getList" icon="el-icon-refresh"></el-button>
          </template>
          <template slot-scope="{row}">
            <el-button type="text" @click="cancelOrder(row.id)" v-if="row.status == 0">取消订单</el-button>
            <el-button type="text" @click="submitError(row)" v-if="row.status == 1 || row.status == 2" style="color:red">申请投诉</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 投诉dialog -->
    <el-dialog title="申请投诉" :visible.sync="dialogVisible" width="50%">
      <div>
        <el-form ref="submitForm" :model="submitForm" label-width="110px" label-position="left">
          <el-form-item label="订单编号：">
            <span>{{submitForm.orderNumber}}</span>
          </el-form-item>
          <el-form-item label="项目名称：">
            <span>{{submitForm.projectName}}</span>
          </el-form-item>
          <el-form-item label="工作人员：">
            <span>{{submitForm.masterName}}</span>
          </el-form-item>
          <el-form-item label="订单创建时间：">
            <span>{{submitForm.createTime}}</span>
          </el-form-item>
          <el-form-item label="描述：">
            <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="submitForm.desc"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="sentError">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getOrderListById, cancelOrder, submitSuggestion, changeStatus } from '../api/index';
import tools from '../utils';
export default {
  name: '',
  props: [''],
  data() {
    return {
      orderList: [],
      dialogVisible: false,
      submitForm: {
        type: '订单',
      },
    };
  },
  components: {},
  computed: {},
  created() {
    this.getList();
  },
  methods: {
    // 获取列表
    getList() {
      let id = JSON.parse(localStorage.getItem('userInfo')).userId;
      getOrderListById(id).then((res) => {
        if (res.code == 200) {
          res.data.forEach((r) => {
            r.date = tools.timestampToTime(r.date).split(' ')[0];
            r.createTime = tools.timestampToTime(r.createTime);
          });
          this.orderList = res.data;
        }
      });
    },
    // 取消订单
    cancelOrder(id) {
      this.$confirm('您确认取消订单？取消后需要重新申请', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          this.$message({
            type: 'success',
            message: '取消成功!',
          });
          cancelOrder(id).then((res) => {
            if (res.code == 200) {
              this.getList();
            }
          });
        })
        .catch(() => {});
    },
    // 信息投诉处理数据
    submitError(item) {
      let user = JSON.parse(localStorage.getItem('userInfo'));
      this.dialogVisible = true;
      this.submitForm.anonymous = 1;
      this.$set(this.submitForm, 'orderNumber', item.orderNumber);
      this.$set(this.submitForm, 'orderId', item.id);
      this.$set(this.submitForm, 'name', user.name);
      this.$set(this.submitForm, 'phone', user.phone);
      this.$set(this.submitForm, 'projectName', item.name);
      this.$set(this.submitForm, 'masterName', item.masterName);
      this.$set(this.submitForm, 'createTime', item.createTime);
    },
    // 提交投诉
    sentError() {
      this.dialogVisible = false;
      this.submitForm.des = `
      订单编号：${this.submitForm.orderNumber}</br>
      项目名称：${this.submitForm.projectName}</br>
      投诉用户：${this.submitForm.name}</br>
      工作人员：${this.submitForm.masterName}</br>
      订单创建时间：${this.submitForm.createTime}</br>
      投诉内容：${this.submitForm.desc}
      `;
      this.submitForm.status = 0;
      submitSuggestion(this.submitForm).then((res) => {
        if (res.code == 200) {
          let obj = {};
          obj.status = 3;
          obj.id = this.submitForm.orderId;
          changeStatus(obj).then((r) => {
            if (r.code === 200) {
              this.$message.success('申诉成功！');
              this.getList();
            }
          });
        }
      });
    },
  },
  watch: {},
};
</script>

<style scoped lang='scss'>
.order-list {
  width: 100%;
  .order-list__title {
    width: 100%;
    height: 220px;
    background: url('../assets/imgs/blue_top.png') no-repeat;
    background-size: cover;
    h2 {
      color: #fff;
      font-size: 50px;
      line-height: 220px;
      text-align: center;
      letter-spacing: 0.5rem;
    }
  }
  .order-list__content {
    margin: 20px 0;
  }
}
</style>